package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

import tw.edu.tku.im.maze.ChangeTeamRep;
import tw.edu.tku.im.maze.ChangeTeamReq;
import tw.edu.tku.im.maze.LeaveRoomRep;
import tw.edu.tku.im.maze.Packet;

public class ChangeTeamProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "changeTeam";
   }

   @Override
   Packet process(Packet packet) {
      ChangeTeamReq req = (ChangeTeamReq)packet;
      try {
         Connection con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
             con.prepareCall("{ ? = call usp_changeTeam(?, ?, ?)}");
         cstmt.registerOutParameter(1, Types.INTEGER);
         cstmt.setInt(2, req.getRoomID());
         cstmt.setString(3, req.getUserID());
         cstmt.setInt(4, req.getTicket());
         cstmt.execute();
         int state = cstmt.getInt(1);
         return new ChangeTeamRep(state);
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return new ChangeTeamRep(2);
   }
}
